package com.ld.security.server.admin.modules.msg.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.base.service.*;
import com.ld.security.server.admin.modules.msg.constant.MsgGroupNewsUserRelConstant;
import com.ld.security.server.admin.modules.msg.entity.MsgGroupNewsEntity;
import com.ld.security.server.admin.modules.msg.entity.MsgGroupUserRelEntity;
import com.ld.security.server.admin.modules.msg.mapping.MsgGroupNewsUserRelMapping;
import com.ld.security.server.admin.modules.msg.mapping.MsgGroupUserRelMapping;
import com.ld.security.server.admin.modules.msg.service.IMsgGroupNewsUserRelService;

import com.ld.security.server.admin.modules.msg.entity.MsgGroupNewsUserRelEntity;

import com.ld.security.server.admin.modules.msg.service.IMsgGroupUserRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class MsgGroupNewsUserRelServiceImpl  extends ISuperServiceImpl<MsgGroupNewsUserRelEntity>
implements
        IMsgGroupNewsUserRelService {

        @Autowired
        IMsgGroupUserRelService iMsgGroupUserRelService;

        @Autowired
        MsgGroupUserRelMapping msgGroupUserRelMapping;


        @Autowired
        MsgGroupNewsUserRelMapping msgGroupNewsUserRelMapping;

        /**
         * 批量推送信息
         * @param entity
         * @return
         */
        @Override
        public int pushMsg(MsgGroupNewsEntity entity) {
                if(StringUtil.isNull(entity.getToUser())){
                        MsgGroupUserRelEntity q =  iMsgGroupUserRelService.newInstance();
                        q.setGroupId(entity.getGroupId());

                        List<MsgGroupUserRelEntity> list =  iMsgGroupUserRelService.all(q);


                        List<MsgGroupNewsUserRelEntity> vs = list.stream().map(v -> {
                                MsgGroupNewsUserRelEntity e =  this.newInstance();
                                e.setGroupId(entity.getGroupId());
                                e.setNewsId(entity.getId());
                                e.setUserId(v.getUserId());
                                e.setMsgStatus(MsgGroupNewsUserRelConstant.msgStatus.no_read);
                                return e;
                        }).collect(Collectors.toList());
                        this.saveBatch(vs);
                        return vs.size();
                }else{
                        MsgGroupNewsUserRelEntity from =  this.newInstance();
                        from.setGroupId(entity.getGroupId());
                        from.setUserId(entity.getFromUser());
                        from.setUserName(entity.getFromUserName());
                        from.setNewsId(entity.getId());

                        MsgGroupNewsUserRelEntity to =  this.newInstance();
                        to.setNewsId(entity.getId());
                        to.setGroupId(entity.getGroupId());
                        to.setUserId(entity.getToUser());
                        to.setUserName(entity.getToUserName());

                        this.saveBatch(Arrays.asList(from,to));
                        return 2;
                }


        }

        @Override
        public int readMsg(List<String> ids) {
                MsgGroupNewsUserRelEntity e =  this.newInstance();
                e.setMsgStatus(MsgGroupNewsUserRelConstant.msgStatus.read);
                UpdateWrapper uw =  new UpdateWrapper<>().in("id",ids);
                return msgGroupNewsUserRelMapping.update(e,uw);

        }
}
